package com.module.app.pop.anim;

import android.animation.FloatEvaluator;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lxj.xpopup.animator.PopupAnimator;
import com.module.base.R;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * Description: 背景模糊动画器
 * Create by dance, at 2018/12/9
 * 跟BlurAnimator 一样，只是效果太差
 */
public class PopBlurAnimator extends PopupAnimator {

    private FloatEvaluator evaluate = new FloatEvaluator();

    public PopBlurAnimator(View target) {
        super(target, 500);
    }

    public Bitmap decorBitmap;
    public ImageView iv_bg;
    public boolean hasShadowBg = false;
    private int blur = 8;
    private int duration = 500;
    private Handler handler = new Handler();

    public PopBlurAnimator(ImageView iv_bg) {
        this.iv_bg = iv_bg;
    }

    @Override
    public void initAnimator() {


    }

    @Override
    public void animateShow() {
        if (!isDestroyed()) {
            if (decorBitmap == null){
                iv_bg.setBackgroundResource(R.color.white);
            } else {
                Glide.with(iv_bg.getContext()).load(decorBitmap)
                        .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, getBlur())))
                        .into(iv_bg);
            }

        }
//        ValueAnimator animator = ValueAnimator.ofInt(0, blur);
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                Glide.with(iv_bg.getContext()).load(decorBitmap)
//                        .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, (Integer) animation.getAnimatedValue())))
//                        .into(iv_bg);
//            }
//        });
//        animator.setInterpolator(new LinearInterpolator());
//        animator.setDuration(XPopup.getAnimationDuration()).start();
    }

    @Override
    public void animateDismiss() {
        handler.removeCallbacksAndMessages(null);
        if (decorBitmap != null && !decorBitmap.isRecycled()) {
            decorBitmap.recycle();
        }
//        ValueAnimator animator = ValueAnimator.ofInt(blur, 0);
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                Glide.with(iv_bg.getContext()).load(decorBitmap)
//                        .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, (Integer) animation.getAnimatedValue())))
//                        .into(iv_bg);
//            }
//        });
//        animator.setInterpolator(new LinearInterpolator());
//        animator.setDuration(XPopup.getAnimationDuration()).start();
    }


    public int getBlur() {
        return blur;
    }

    public void setBlur(int blur) {
        this.blur = blur;
    }

    @Override
    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    private boolean isDestroyed() {
        if (iv_bg != null && iv_bg.getContext() != null) {
            if (iv_bg.getContext() instanceof Activity && ((Activity) iv_bg.getContext()).isDestroyed()) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
}
